//
//  WSFavoritesController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/2.
//

#import "WSFavoritesController.h"
#import "WSFavoritesCell.h"
#import "WSFavoritesRequest.h"
#import "UIButton+ImageAlign.h"
#import "WSChannel.h"
#import "CTMediator+WSPlayerAction.h"

@interface WSFavoritesController () <UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *editBarBottom;
@property (weak, nonatomic) IBOutlet UIButton *allSelectBtn;

@property (nonatomic, assign) BOOL isEdit;

@end

@implementation WSFavoritesController

- (void)viewDidLoad {
    [super viewDidLoad];
    WSFavoritesRequest * request = [WSFavoritesRequest new];
    self.request = request;
    [self.allSelectBtn setButtonImageTitleStyle:(WSButtonImageTitleStyleLeft) padding:10];
    [self setEmptyStateImage:@"img_wusc" state:ListViewEmptyStateEmpty];
    [self setEmptyStateTitle:APPLocalized(@"很抱歉，暫無收藏內容") state:ListViewEmptyStateEmpty];
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}

- (void)beginEdit {
    self.isEdit = YES;
}

- (NSInteger)dataArrayCount {
    return self.dataArray.count;
}

- (IBAction)allSelectAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    for (WSVideo * video in self.dataArray) {
        video.isSelected = sender.selected;
    }
    [self.listView reloadData];
}

- (IBAction)cancelAction:(id)sender {
    self.isEdit = NO;
}

- (IBAction)deleteAction:(id)sender {
    NSMutableArray * delArray = [NSMutableArray new];
    for (WSVideo * video in self.dataArray) {
        if (video.isSelected) {
            [delArray addObject:video];
        }
    }
    if (delArray.count > 0) {
        UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_AlterWithTitle:APPLocalized(@"確定要將數據從收藏列表移除嗎？") content:@"" confirm:APPLocalized(@"刪除") confirmHandle:^{
            [self delVideoRequest:delArray];
        } cancel:@""];
        target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        target.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:target animated:YES completion:nil];
        
    }
    else {
        self.isEdit = NO;
    }
}

- (void)delVideoRequest:(NSArray*)array {
    [WSToast loading];
    [WSFavoritesRequest delcharonTV_FavoriteWithTVIds:array success:^{
        [WSToast dismiss];
        [self.dataArray removeObjectsInArray:array];
        self.isEdit = NO;
        [self benginRefresh];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {

    }];
}

- (void)initArraySelectStatus {
    for (WSVideo * video in self.dataArray) {
        video.isSelected = NO;
    }
}
#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSFavoritesCell * favoritesCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSFavoritesCell" forIndexPath:indexPath];
    [favoritesCell setDataWithObject:self.dataArray[indexPath.row] edit:self.isEdit];
    return favoritesCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = floor((WSScreenWidth - 12 - 12 - 10 - 10) / 3.0f);
    size.height = size.width * 4 / 3.0f + 36;
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsMake(25, 12, 0, 12);
    if (self.isEdit) {
        inset.bottom = 58;
    }
    return inset;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WSVideo * video = self.dataArray[indexPath.row];
    if (self.isEdit) {
        video.isSelected = !video.isSelected;
        [self.listView reloadData];
    }
    else {
        UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PlayerWithVideo:video];
        [self.navigationController pushViewController:target animated:YES];
    }
    
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    self.editBarBottom.constant = isEdit?0:-80;
    [self.view uploadLayout];
    if (isEdit) {
        [self initArraySelectStatus];
    }
    else {
        if ([self.delegate respondsToSelector:@selector(favoritesControllerExitEdit)]) {
            [self.delegate favoritesControllerExitEdit];
        }
    }
    [self.listView reloadData];
}

@end
